//
//  NewCustomerWebViewController.m
//  EMMApp
//
//  Created by edz on 2019/7/31.
//  Copyright © 2019 YiXun. All rights reserved.
//

#import "NewCustomerWebViewController.h"
#import "CustomerModel.h"

static NSString *NewCustomerPath =@"appapi/customer_add/";
static NSString *EditCustomerPath =@"appapi/customer_edit/";

@interface NewCustomerWebViewController ()
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,strong) NSURLRequest *urlRequest;
@end

@implementation NewCustomerWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self creatNavigationController];
}


-(void)setupUI{
    self.webView =[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight)];
    [self.view addSubview:self.webView];
    
    NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
    NSString *terminalCode = [IDTools devId2];
    NSString *merchantId = [userdf objectForKey:@"merchantId"];
    NSString *projectId = [userdf objectForKey:@"projectId"];
    NSString *version = apiVString;
    NSString *busNumType = @"11";
    NSString *busNum = @"01";
    NSString *token = [userdf objectForKey:@"is_login"];
    NSString *userId = [userdf objectForKey:@"userNameId"];
    NSString *projectCode = [userdf objectForKey:@"projectCode"];
    
    CustomerModel *paramsModel =[[CustomerModel alloc]init];
    paramsModel.params =[[bodyModel alloc]init];
    paramsModel.params.terminalCode =terminalCode;
    paramsModel.params.merchantId =merchantId;
    paramsModel.params.projectId =projectId;
    paramsModel.params.version =version;
    paramsModel.params.busNumType =busNumType;
    paramsModel.params.busNum =busNum;
    paramsModel.params.token =token;
    paramsModel.params.userId =userId;
    paramsModel.params.projectCode =projectCode;
    paramsModel.params.customerId =self.customerId;
    NSString *params = [paramsModel toJSONString];
    NSString *UrlStr;
    if (_isflg ==NO) {
       UrlStr =[NSString stringWithFormat:@"%@/%@?body=%@",apiBaseUrl,NewCustomerPath,params];
    }else{
        UrlStr =[NSString stringWithFormat:@"%@/%@?body=%@",apiBaseUrl,EditCustomerPath,params];
    }
    
    NSString *destionURL = [UrlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *URL =[NSURL URLWithString:destionURL];
    [self.webView loadRequest:[NSURLRequest requestWithURL:URL]];
    self.urlRequest = [NSURLRequest requestWithURL:URL];
}

- (void)creatNavigationController {
    [self updateNavBar];
    if (_isflg ==NO) {
         self.navigationItem.title = @"新增客户";
    }else{
         self.navigationItem.title = @"编辑客户";
    }
   
    //返回按钮初始化
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:CGRectMake(0, 0, 54 ,44)];
    [backBtn addTarget:self
                action:@selector(NewbackToPreviousPage:)
      forControlEvents:UIControlEventTouchUpInside];
    [backBtn setBackgroundColor:[UIColor clearColor]];
    [backBtn setImage:[UIImage imageNamed:@"ico_back"] forState:UIControlStateNormal];
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    if (EMM_WIDTH <= 320) {
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    }else if (EMM_WIDTH > 375){
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    }else{
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}
#pragma mark - 返回按钮调用方法
- (void)NewbackToPreviousPage:(id) send {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您的信息尚未保存，是否确认放弃？" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        [self NewCreatUpExit];//退出
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
- (void)NewCreatUpExit {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.webView loadRequest:self.urlRequest];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
